/**
 * 
 */
package com.byzk.p2p.admin.task;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.byzk.p2p.admin.base.util.BaseUtil;
import com.byzk.p2p.admin.borrow.dao.BorrowDao;
import com.byzk.p2p.admin.borrow.service.BorrowService;
import com.byzk.p2p.admin.borrow.vo.BorrowVo;
import com.byzk.p2p.admin.money.dto.BorrowPaymentDetail;
import com.byzk.p2p.admin.system.dto.MemberLevel;
import com.byzk.p2p.admin.system.service.MemberService;
import com.byzk.p2p.admin.user.dto.UserAccount;
import com.byzk.p2p.admin.user.service.UserAccountService;

/***
 * <p>
 * Title:UpdateFee
 * </p>
 * <p>
 * Description: 更新用户利息管理费
 * </p>
 * <p>
 * Company:xbd
 * </p>
 * 
 * @author:Administrator
 * @date:2016年9月27日下午4:39:03
 */
@Component
public class UpdateFee {

	@Autowired
	private MemberService memberService;
	@Autowired
	private UserAccountService accountService;
	@Autowired
	private BorrowDao borrowDao;
	@Autowired
	private BorrowService borrowService;

	private static Logger logger = Logger.getLogger(UpdateFee.class);

	/**
	 * 
	 * <p>
	 * Description:更新投资用户利息管理费
	 * </p>
	 * <p>
	 * Company:xbd
	 * </p>
	 * 
	 * @author:lilei
	 * @date:2016年9月27日下午4:40:54
	 */
//	@Scheduled(cron = "0 44 11 * * ? ")
	public void updateTenderUserFee() {
		List<BorrowVo> payBorrowList = borrowService.payBorrowList();
		if (payBorrowList != null && payBorrowList.size() > 0) {
			int noteIndex = payBorrowList.size();
			for (BorrowVo borrow : payBorrowList) {
				logger.info("还有【"+noteIndex+"】条标记录未操作----当前标：" + borrow.getTitle());
				int i = 1;
				// 根据投标编号查看换款下详情信息
				List<BorrowPaymentDetail> detailList = borrowDao.paymentDetailByBorrowId(borrow.getId());
				for (BorrowPaymentDetail detailItem : detailList) {
					logger.info("更新投资用户利息管理费----正在处理第【" + i + "】------笔数据");
					/**
					 * 计算利息管理费
					 */
					UserAccount account = accountService.getByUserId(detailItem.getUserId());
					MemberLevel level = memberService.getById(account.getLevel());
					double discount = 0.0;
					if (level == null) {
						discount = 0.1;
					} else {
						discount = level.getFeePercent();
					}
					BorrowPaymentDetail borrowDetail = new BorrowPaymentDetail();
					borrowDetail.setUserId(detailItem.getUserId());
					borrowDetail.setId(detailItem.getId());
					borrowDetail.setFee(BaseUtil.StringToDoubletoFixed2(detailItem.getNotPaymentInterest() * discount));
					borrowDao.updateBorrowDetailByTid(borrowDetail);
					logger.info("更新投资用户利息管理费----处理第【" + i + "】------笔数据更新成功！利息为：【" + detailItem.getNotPaymentInterest()
							+ "】 利息管理费权重为【"+discount+"】利息管理费为：" + borrowDetail.getFee() + "投资用户编号为：【" + detailItem.getUserId() + "】");
					i++;
				}
				
				noteIndex--;
			}
			
		}
	}
}
